package com.didi.gulimall.cart.controller;

import com.didi.gulimall.cart.service.CartService;
import com.didi.gulimall.cart.vo.Cart;
import com.didi.gulimall.cart.vo.CartItem;
import com.didi.gulimall.common.exception.DuplicateFieldsException;
import com.didi.gulimall.common.utils.LogUtils;
import com.didi.gulimall.common.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @author 10626
 */
@Api("购物车")
@Controller
@Slf4j
public class CartController {

    @Autowired
    private CartService cartService;

    @Value("${gulimall.item}")
    private String itemHtml;

    @Value("${gulimall.cart}")
    private String cartHtml;

    @Value("${gulimall.home}")
    private String homeHtml;

    @GetMapping("/cartList.html")
    public String getCartList(Model model) throws ExecutionException, InterruptedException {
        return cartService.getCartList(model);
    }

    @ResponseBody
    @GetMapping("member/list")
    public R memberList() {
        return cartService.memberCartItems();
    }

    @ResponseBody
    @GetMapping("member/list/no/updateproduct")
    public R memberListNoUpdateProduct() {
        return cartService.memberListNoUpdateProduct();
    }

    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer num) {
        cartService.changeItemCount(skuId, num);
        return "redirect:" + cartHtml + "cartList.html";
    }

    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check) {
        cartService.checkItem(skuId, check);
        return "redirect:" + cartHtml + "cartList.html";
    }

    @GetMapping("/removeItem")
    public String removeItem(@RequestParam("skuId") Long skuId) {
        cartService.removeItem(skuId);
        return "redirect:" + cartHtml + "cartList.html";
    }
    @ResponseBody
    @PostMapping("order/delete")
    public R orderRemoveItem(@RequestBody List<CartItem> items){
        return cartService.orderRemoveItem(items);
    }


    @GetMapping("/addCart")
    public String addCart(
            @ApiParam(name = "skuId", value = "skuId", required = true)
            @RequestParam("skuId") Long skuId,
            @ApiParam(name = "num", value = "数量", required = true)
            @RequestParam("num") Integer num,
            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        try {
            cartService.addToCart(skuId, num);
            redirectAttributes.addAttribute("skuId", skuId);
            return "redirect:" + cartHtml + "/addToCartSuccess.html";
        } catch (DuplicateFieldsException e) {
            log.error("出错了{}", LogUtils.getStackTraceInfo(e));
            log.error("错误信息{}", e.getMessage());
            return "redirect:" + itemHtml + skuId + ".html";
        }
    }
    @GetMapping("/addCart")
    public String addCartCache(
            @ApiParam(name = "skuId", value = "skuId", required = true)
            @RequestParam("skuId") Long skuId,
            @ApiParam(name = "num", value = "数量", required = true)
            @RequestParam("num") Integer num,
            RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        try {
            cartService.addCartCache(skuId, num);
            redirectAttributes.addAttribute("skuId", skuId);
            return "redirect:" + cartHtml + "/addToCartSuccess.html";
        } catch (DuplicateFieldsException e) {
            log.error("出错了{}", LogUtils.getStackTraceInfo(e));
            log.error("错误信息{}", e.getMessage());
            return "redirect:" + itemHtml + skuId + ".html";
        }
    }

    @GetMapping("addToCartSuccess.html")
    public String addToCartSuccess(
            @ApiParam(name = "skuId", value = "skuId", required = true)
            @RequestParam("skuId") Long skuId,
            Model model) {
        CartItem cartItem = cartService.getCartItem(skuId);
        if (cartItem == null) {
            return "redirect:" + itemHtml + skuId + ".html";
        }
        model.addAttribute("cartItem", cartItem);
        return "success";
    }

}
